package com.wx.spider.sql.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.wx.spider.common.model.CommonResponse;
import com.wx.spider.common.pojo.db.WxBizNews;
import com.wx.spider.sql.service.WxBizNewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;


/**
 * @author:feng
 * @create:2021-04-25 16:36
 */
@RestController
@RequestMapping("/wxbiznews")
public class WxBizNewsController {
    @Autowired
    private WxBizNewsService wxBizNewsService;
    
    @GetMapping(value = "/getCountByLink")
    public CommonResponse<Integer> getCountByLink(String link){
        try{
            QueryWrapper<WxBizNews> queryWrapper = new QueryWrapper<WxBizNews>();
            queryWrapper.eq("content_url", link).eq("delflag", 0);
            int count = wxBizNewsService.count(queryWrapper);
            Integer res = Integer.valueOf(count);
            return CommonResponse.succeed(res);
        }catch (Exception e) {
            e.printStackTrace();
            return CommonResponse.INTERNAL_ERROR_RESPONSE;
        }
    }

    @GetMapping(value = "/findById")
    public CommonResponse<WxBizNews> findById(Long id){
        try{
            QueryWrapper<WxBizNews> queryWrapper = new QueryWrapper<WxBizNews>();
            queryWrapper.eq("id", id).eq("delflag", 0);
            return CommonResponse.succeed(wxBizNewsService.getOne(queryWrapper));
        }catch (Exception e) {
            e.printStackTrace();
            return CommonResponse.INTERNAL_ERROR_RESPONSE;
        }
    }
    

    @PostMapping("/save")
    private CommonResponse<WxBizNews> save(@RequestBody WxBizNews wxBizNews){
        try{

            boolean flag = wxBizNewsService.save(wxBizNews);
            if(flag){
                return CommonResponse.succeed(wxBizNews);
            }else{
                return CommonResponse.INTERNAL_ERROR_RESPONSE;
            }
        }catch (Exception e) {
            e.printStackTrace();
            return CommonResponse.INTERNAL_ERROR_RESPONSE;
        }
    }

    @PutMapping("/update")
    private CommonResponse<WxBizNews> update(@RequestBody WxBizNews wxBizNews){
        try{

            boolean flag = wxBizNewsService.updateById(wxBizNews);
            if(flag){
                return CommonResponse.succeed(wxBizNews);
            }else{
                return CommonResponse.INTERNAL_ERROR_RESPONSE;
            }
        }catch (Exception e) {
            e.printStackTrace();
            return CommonResponse.INTERNAL_ERROR_RESPONSE;
        }
    }
    @PutMapping("/updateAppmsg")
    private CommonResponse<Boolean> updateAppmsg(@RequestBody Map<String, Integer> appmsgMap, @RequestParam("biz") String biz,@RequestParam("sn") String sn){
        try{
            UpdateWrapper<WxBizNews> wrapper = new UpdateWrapper<>();

            wrapper.set("read_num", appmsgMap.get("readNum"));
            wrapper.set("like_num", appmsgMap.get("likeNum"));
            wrapper.set("onlook_num", appmsgMap.get("onlookNum"));
            wrapper.set("is_appmsg", 1);

            wrapper.eq("biz", biz).eq("sn", sn);

            boolean flag = wxBizNewsService.update(wrapper);
            return CommonResponse.succeed(flag);
        }catch (Exception e) {
            e.printStackTrace();
            return CommonResponse.INTERNAL_ERROR_RESPONSE;
        }
    }
}
